package com.bookRecommend.controller;


import com.bookRecommend.commons.ResponseUtils;
import com.bookRecommend.model.Book;
import com.bookRecommend.model.Score;
import com.bookRecommend.service.IBookService;
import com.bookRecommend.service.IScoreService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController {
    @Resource
    IBookService service;
    @Resource
    IScoreService scoreService;


    @RequestMapping
    public ResponseUtils findAll(Integer pageNum, Integer size, String bookName) {
        PageInfo<Book> pageInfo = service.findPage(pageNum, size, bookName, null);
        if (pageInfo != null) {
            return ResponseUtils.success("", pageInfo);
        }
        return ResponseUtils.failure("查找失败！");
    }


    @GetMapping("/{bookName}")
    public ResponseUtils findByName(@PathVariable String bookName) {
        List<Book> books = service.findByBook(bookName);
        if (books != null) {
            return ResponseUtils.success("", books);
        }
        return ResponseUtils.failure("查找失败！");
    }

    @RequestMapping("/update")
    public ResponseUtils modify(@RequestBody Book book) {
        int flag = service.modifyByBook(book);
        if (flag > 0) {
            return ResponseUtils.success("修改成功！");
        }
        return ResponseUtils.failure("修改失败！");
    }

    @RequestMapping("/hot")
    public ResponseUtils hot() {
        List<Book> hot = service.getHot();
        if (hot != null) {
            return ResponseUtils.success("",hot);
        }
        return ResponseUtils.failure("查询失败");
    }

    /*
     * 评分次数最多的
     */
    @RequestMapping("/hotScore")
    public ResponseUtils hotScore() {
        List<Book> hotScore = service.getHotScore();
        if (hotScore != null) {
            return ResponseUtils.success("",hotScore);
        }
        return ResponseUtils.failure("查询失败");
    }

    // 根据用户评分习惯进行推荐，根据最近评分高分进行推荐
    // 根据用户评分的次数进行推荐 -- 查询当前登录用户的评分次数最多的类型
    @RequestMapping("/recommend/hotScore")
    public ResponseUtils findUserScore(Integer userId) {
        Score hotScore = scoreService.findHotScoreByUserId(userId);
        if (hotScore != null) {
            // 通过评分表里的 bookId 查询图书
            Book book = service.findByBookId(hotScore.getBookId());
            if (book ==  null) {
                return ResponseUtils.failure("暂无推荐！");
            }
            // 通过图书类型查询图书进行展示
            PageInfo<Book> recommendPage = service.findPage(1, 6, null, book.getTypeId());
            if (recommendPage != null) {
                return ResponseUtils.success("推荐成功！", recommendPage);
            }
        }
        return ResponseUtils.failure("暂无推荐！");
    }

    // 根据其他用户的高评分进行推荐
    @RequestMapping("/recommend/hot")
    public ResponseUtils findHot() {
        return hot();
    }

}
